var d_tid = GetQueryString("d_tid");
var d_wcid = GetQueryString("d_wcid");
var d_tpid = GetQueryString("d_tpid");
var w_did = GetQueryString("w_did");
var tech_id;

//显示版本号
var version = [];
//显示基本信息
var infomation = [];
for (let i = 0; i < 2; i++) {
    infomation[i] = document.getElementById("infomation" + i).children[0];
}
var infomationData = [
    [["产品名称", "部件名称", "项目名称", "检测部位", "热处理状况", "检测时机", "检测标准", "技术等级"], []]
];
$.ajax({
    url: '/op/operation',
    type: 'post',
    data: {
        "requestType": "initBaseData",
        "d_wcid": d_wcid,
        "d_tid": d_tid
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
console.log(postData);

infomationData[0][1] = [
    postData[0].task_product,
    postData[0].wc_pipeName,
    postData[0].task_pname,
    postData[0].task_position,
    postData[0].task_heat,
    postData[0].task_opportunity,
    postData[0].task_standard,
    postData[0].task_level
];
tech_id = postData[0].tech_id;
console.log(tech_id);
document.getElementById("wc_weldM").value = postData[0].wc_weldM;
document.getElementById("wc_texture").value = postData[0].wc_texture;
document.getElementById("tech_focus").value = postData[0].tech_focus;
document.getElementById("tech_filmV").value = postData[0].tech_filmV;
document.getElementById("tech_devTime").value = postData[0].tech_devTime;
document.getElementById("tech_devT").value = postData[0].tech_devT;
document.getElementById("wc_scale").value = postData[0].wc_scale;
document.getElementById("op_photo").value = user.user_name;
for (let j = 0; j < 1; j++) {
    for (let i = 0; i < infomationData[j][0].length; i++) {
        let tr = document.createElement("tr");
        if (i % 3 === 0) {
            infomation[j].appendChild(tr);
        }
        let td = document.createElement("td");
        td.setAttribute("class", "td-shadows");
        td.setAttribute("align", "center");
        td.innerHTML = infomationData[j][0][i];
        infomation[j].children[infomation[j].childElementCount - 1].appendChild(td);
        td = document.createElement("td");
        td.setAttribute("align", "center");
        td.innerHTML = infomationData[j][1][i];
        infomation[j].children[infomation[j].childElementCount - 1].appendChild(td);
    }
}

var workpieceTitle = ["部件/管线编号", "工件/焊缝编号", "实际规格", "管电压", "管电流/源活度", "曝光时间", "透照次数",
    "透照方式", "像质计型号", "摆放位置"];
var workpieceTable = document.getElementById("infomation1").children[1];
var workpieceInfo = [];
$.ajax({
    url: '/op/operation',
    type: 'post',
    data: {
        "requestType": "initWorkpieceData",
        "w_did": w_did
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
console.log(postData);

for (let i = 0; i < postData.length; i++) {
    workpieceInfo.push([
        postData[i].w_id,
        postData[i].w_pipeNum,
        postData[i].w_weldNum
    ]);
}
//console.log(postData.length);//DDF
for (let i = 0; i < workpieceTitle.length; i++) {
    let th = document.createElement("th");
    th.innerHTML = workpieceTitle[i];
    infomation[1].children[0].appendChild(th);
}
for (let i = 0; i < workpieceInfo.length; i++) {
    let tr = document.createElement("tr");
    for (let j = 1; j < 3; j++) {
        let td = document.createElement("td");
        td.innerHTML = workpieceInfo[i][j];
        tr.appendChild(td);
    }
    for (let j = 2; j < workpieceTitle.length; j++) {
        if (j === 3) {
            let select = document.createElement("select");
            select.setAttribute("class", "custom-select d-block w-100");
            select.innerHTML =
                "<option>160</option>"
                + "<option>170</option>"
                + "<option>180</option>"
                + "<option>190</option>"
                + "<option>200</option>"
                + "<option>210</option>"
                + "<option>220</option>"
                + "<option>230</option>"
                + "<option>240</option>"
                + "<option>250</option>"
                + "<option>260</option>"
                + "<option>270</option>"
                + "<option>280</option>"
                + "<option>290</option>";
            let td = document.createElement("td");
            td.appendChild(select)
            tr.appendChild(td);
        }
        else if (j === 4) {
            let select = document.createElement("select");
            select.setAttribute("class", "custom-select d-block w-100");
            select.innerHTML =
                "<option>5 mA</option>"
                + "<option>100Ci</option>"
                + "<option>90Ci</option>"
                + "<option>80Ci</option>"
                + "<option>70Ci</option>"
                + "<option>60Ci</option>"
                + "<option>50Ci</option>";
            let td = document.createElement("td");
            td.appendChild(select)
            tr.appendChild(td);
        }
        else if (j === 5) {
            let select = document.createElement("select");
            select.setAttribute("class", "custom-select d-block w-100");
            select.innerHTML =
                "<option>1min</option>"
                + "<option>2min</option>"
                + "<option>3min</option>"
                + "<option>4min</option>"
                + "<option>5min</option>";
            let td = document.createElement("td");
            td.appendChild(select)
            tr.appendChild(td);
        }
        else if (j === 6) {
            let select = document.createElement("select");
            select.setAttribute("class", "custom-select d-block w-100");
            select.innerHTML = 
                "<option>2</option>"
                + "<option>3</option>"
                + "<option>4</option>"
                + "<option>5</option>"
                + "<option>6</option>"
                + "<option>7</option>"
                + "<option>8</option>";
            let td = document.createElement("td");
            td.appendChild(select)
            tr.appendChild(td);
        } 
        else if (j === 7) { // 透照方式
            let select = document.createElement("select");
            select.setAttribute("class", "custom-select d-block w-100");
            select.innerHTML = 
                "<option>单壁单影</option>"
                + "<option>环缝源在外</option>"
                + "<option>环缝源在内</option>"
                + "<option>双壁单影</option>"
                + "<option>中心透照</option>"
                + "<option>双壁双影</option>"
                + "<option>垂直透照</option>";
            let td = document.createElement("td");
            td.appendChild(select)
            tr.appendChild(td);
        }
        else {
            let th = document.createElement("td");
            th.innerHTML = "<input style='width: 80px'/>";
            tr.appendChild(th);
        }
    }
    workpieceTable.appendChild(tr);
}

function GetInputs() {
    var input = [[],[],[],[],[],[],[],[],[],[]];
    for (let i = 0; i < workpieceInfo.length; i++) {
        input[0][i] = workpieceInfo[i][0];
        for (let j = 2; j < workpieceTitle.length; j++) {
            input[j - 1][i] = workpieceTable.children[i].children[j].children[0].value;
        }
    }
    return input;
}

function OnCommit() {
    //前屏 后屏那里必须写一个数字上去，底下两个日期 都不能为空
    if (document.getElementById("op_pdate").value === "") {
        alert("拍片日期不能为空");
        return;
    }
    if (document.getElementById("op_wdate").value === "") {
        alert("洗片日期不能为空");
        return;
    }


    $.ajax({
        url: '/op/operation',
        type: 'post',
        data: {
            "requestType": "updateBaseData",
            "op_tid": d_tid,
            "op_techid": tech_id,
            "d_id": w_did,
            "op_weldM": document.getElementById("wc_weldM").value,
            "op_texture": document.getElementById("wc_texture").value,
            "op_focus": document.getElementById("tech_focus").value,
            "op_equ": document.getElementById("op_equ").value,
            "op_equid": document.getElementById("op_equid").value,
            "op_sensitizer": document.getElementById("op_sensitizer").value,
            "op_filmV": document.getElementById("tech_filmV").value,
            "op_filmBrand": document.getElementById("op_filmBrand").value,
            "op_liquor": document.getElementById("op_liquor").value,
            "op_devTime": document.getElementById("tech_devTime").value,
            "op_devT": document.getElementById("tech_devT").value,
            "op_scale": document.getElementById("wc_scale").value,
            "op_remark": document.getElementById("op_remark").value,
            "op_photo": document.getElementById("op_photo").value,
            "op_pdate": document.getElementById("op_pdate").value,
            "op_wash": document.getElementById("op_wash").value,
            "op_wdate": document.getElementById("op_wdate").value,
            "op_front": document.getElementById("op_front").value,
            "op_rear": document.getElementById("op_rear").value
        },
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    let input = GetInputs();
    let info;
    $.ajax({
        url: '/op/operation',
        type: 'post',
        data: {
            "requestType": "selectClient",
            "d_tid": d_tid
        },
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                info = data;
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    $.ajax({
        url: '/op/operation',
        type: 'post',
        data: {
            "requestType": "updateWorkpieceData",
            "w_id": input[0],
            "w_rspec": input[1],
            "w_V": parseFloat(input[2]),
            "w_A": parseFloat(input[3]),
            "w_time": parseFloat(input[4]),
            "w_phoNum": input[5],
            "w_tranType": input[6],
            "w_IQIV": input[7],
            "w_pos": input[8],
            "ph_client": info[0].task_uid,
            "user_part": info[0].task_department,
            "num": input[0].length
        },
        async: false,
        traditional: true,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    let date = new Date();
    $.ajax({
        url: '/op/operation',
        type: 'post',
        data: {
            "requestType": "updateState",
            "d_id": w_did,
            "d_fin": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "d_tpid": d_tpid
        },
        async: false,
        traditional: true,
        success: function (data, status) {
            if (status == 'success') {
                alert("提交成功");
                history.back();
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
}
